package cn.gson.springboot.controller.salecontroller;

import cn.gson.springboot.aop.MyLog;
import cn.gson.springboot.model.pjos.*;
import cn.gson.springboot.model.service.saleservice.ContractService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.sql.Timestamp;

@Controller
public class ContractController {
    @Autowired
    ContractService contractService;

    /**
     * 合同查询
     * int empid
     * @return
     */
    @RequestMapping("/contract_info")
    @ResponseBody
    public Map<String,Object> contractInfo(Integer page,Integer size,Integer type,String name,Integer empId,Integer deptId){
        System.err.println("page"+page);
        System.err.println("size"+size);
        System.err.println("type"+type);
        System.err.println("name"+name);
        System.err.println("empId"+empId);
        System.err.println("deptId"+deptId);
        Page<Object> pageinfo= PageHelper.startPage(page,size);
        contractService.contractsInfo(deptId,empId,type,name);
        Map<String,Object> map = new HashMap<>();
        map.put("toorder_contrtal",pageinfo.getTotal());
        map.put("rows",pageinfo.getResult());
        return map;
    }


    /**
     * 合同详情
     * @param contractid
     * @return
     */
    @RequestMapping("/contract_Details")
    @ResponseBody
    public Contract contractDetails(Integer contractid){
        Contract contract = contractService.contractDetails(contractid);
        return contract;
    }

    /**
     * 商机查询
     * @param coustomId
     * @return
     */
    @RequestMapping("/opportunity_detail")
    @ResponseBody
    public List<BusinessInfo> opportunityDetail(Integer coustomId){
        List<BusinessInfo> businessInfos = contractService.opportunityList(coustomId);
        return businessInfos;
    }

    /**
     * 报价单查询
     * @param customId
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("contoffer_detail")
    @ResponseBody
    public Map<String,Object> contOfferDetail(Integer customId,Integer busId,Integer page,Integer size){
        System.err.println("contOfferDetail >>>>>>"+customId);
        Page<Object> obj=PageHelper.startPage(page,size);
        List<Offer> offers = contractService.contOfferList(customId,busId);
        Map<String,Object> offer = new HashMap<>();
        offer.put("rows",obj.getResult());
        offer.put("total",obj.getTotal());
        return offer;
    }

    /**
     * 付款方式查询
     * @return
     */
    @RequestMapping("Payment_detail")
    @ResponseBody
    public List<TermsPayment> paymentDetail(){
        List<TermsPayment> termsPayments = contractService.paymentList();
        return termsPayments;
    }

    /**
     * 新增合同
     *orderInfos
     * @param request
     * @return
     */
    @MyLog(type = "合同",value = "新增了合同")
    @RequestMapping("contract_save")
    @ResponseBody
    public int saveBusinessInfo(HttpServletRequest request){
        //   Tab tab = JSON.parseObject(request.getParameter("tab"),Tab.class);
        Contract contract1 = JSON.parseObject(request.getParameter("cont"),Contract.class);
        //contract1.getCustomerByCustomId().setCustomId(138);
        System.err.println("指定客户信息"+contract1.getCustomerByCustomId().getCustomId());
        System.err.println("指定客户信息"+contract1.getCustomerByCustomId().getCusName());
        contract1.setContractAppstate(2);//待审核
        int save = contractService.save(contract1);
        return save;
    }


    /**
     * 订单下合同查询
     * int empid
     * @return
     */
    @RequestMapping("/order_contr")
    @ResponseBody
    public Map<String,Object> orderContrac(Integer page,Integer size,String name,Integer userId){
        Page<Object> pageinfo= PageHelper.startPage(page,size);
        List<Contract> contracts = contractService.orderContractInfo(userId,name);
        Map<String,Object> map = new HashMap<>();
        map.put("total",pageinfo.getTotal());
        map.put("rows",pageinfo.getResult());
        return map;
    }


    @RequestMapping("/contract_name")
    @ResponseBody
    public String selectMaxContractName(){
        String s = contractService.selectMaxContractName();
        return s;
    };

    @RequestMapping("/order_name")
    @ResponseBody
    public String selectMaxOrderName(){
        String s = contractService.selectMaxOrderName();
        return s;
    }


    @RequestMapping("/zfhuodan_name")
    @ResponseBody
    public String selectMaxZfhuodanName(){
        String s = contractService.selectMaxZfhuodanName();
        return  s;
    };

}
